package pers.baiban.sqltoword.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.word.TableUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.baiban.sqltoword.config.GenConfig;
import pers.baiban.sqltoword.enums.FontSize;
import pers.baiban.sqltoword.handler.GenFilter;
import pers.baiban.sqltoword.inter.GenTemplate;
import pers.baiban.sqltoword.vo.ColumnVO;
import pers.baiban.sqltoword.vo.TableVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 强子初
 * @since 2023/11/27
 */
@Component
public class WordUtil implements GenTemplate {

    private static GenConfig config;

    private static GenFilter genFilter;

    @Autowired
    WordUtil(GenConfig config, GenFilter genFilter) {
        this.config = config;
        this.genFilter = genFilter;
    }

    // 生成目录
    public static void genMenu(List<Object> data) {

    }

    /**
     * 生成内容
     */
    public static void generateContext(XWPFDocument document, List<TableVO> source) {
        for (TableVO table : source) {
            generateContext(document, table);
        }
    }


    /**
     * 生成一个内容
     */
    public static void generateContext(XWPFDocument document, TableVO source) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        XWPFTable table = TableUtil.createTable(document);
        /* 标题 */
        run.setText(StrUtil.indexedFormat(config.getTableParam().getTitleTextFormat(), source.getNumber(), source.getTableComment(), source.getTableName()));
        run.setFontFamily(config.getTableParam().getTitleFontFamily());
        run.setFontSize(config.getTableParam().getTitleFontSize());

        /* 表格 */
        table.setWidthType(TableWidthType.PCT);
        table.setWidth("100%");
        TableUtil.writeRow(table.createRow(), config.getTableParam().getTableHeader(), false);
        for (ColumnVO column : source.getColumns()) {
            /* 指定列顺序 */
            Map<String, Object> columnMap = BeanUtil.beanToMap(column);
            List<String> showFields = new ArrayList<>();
            config.getTableStyle().getTableParam().getTableColumn().forEach(it ->
                    showFields.add(Optional.ofNullable(columnMap.get(it))
                            .map(Object::toString)
                            .orElse(""))
            );
            TableUtil.writeRow(table.createRow(), showFields, false);
        }
        for (XWPFTableRow row : table.getRows()) {
            for (int i = 0; i < row.getTableCells().size(); i++) {
                XWPFTableCell cell = row.getCell(i);
                cell.setWidthType(TableWidthType.PCT);
                cell.setWidth(config.getTableStyle().getTableParam().getTableHeaderWidth().get(i));
            }
        }
        // 结尾空一行
        document.createParagraph();

        /* 表格样式 */
        genFilter.doHandler(table, config.getTableStyle().getTableParam());
    }
}